package com.wf.ew.orderB.controller;

import com.wf.ew.common.BaseController;
import com.wf.ew.common.JsonResult;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.PoiUtil;
import com.wf.ew.orderB.model.Bill;
import com.wf.ew.orderB.service.BillService;
import net.sf.json.JSONArray;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 *
 * controller
 * @author : Doman
 * @date : 2020-04-13 13:50:45
 * @version ：1.0
 */

@Controller
@RequestMapping("/orderB/bill")
public class BillController extends BaseController {

	@Autowired
	private BillService billService;
	
	/**
	 * 页面
	 **/
	@RequiresPermissions("bill:view")
	@RequestMapping()
	public String bill() {
		return "orderB/bill.html";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("bill:view")
	@RequestMapping(value = "/list", produces = {"text/html;charset=UTF-8;"})
	@ResponseBody
	public String list(PageResult<T> page, Bill bill){
		return billService.list(page, bill,getLoginUser());
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("bill:view")
	@RequestMapping(value = "/ajaxList", produces = {"text/html;charset=UTF-8;"})
	@ResponseBody
	public String ajaxList(Bill bill){
		return JSONArray.fromObject(billService.list(bill,getLoginUser())).toString();
	}

	/**
	 * 新增
	 **/
	@RequiresPermissions("bill:add")
	@ResponseBody
	@RequestMapping("/add")
	public JsonResult add(Model model, Bill bill) {
		return billService.add(bill, getLoginUser());
	}

	/**
	 * 修改
	 **/
	@RequiresPermissions("bill:update")
	@ResponseBody
	@RequestMapping("/update")
	public JsonResult update(Model model, Bill bill) {
		return billService.update(bill, getLoginUser());
	}

	/**
	 * 批量删除
	 **/
	@RequiresPermissions("bill:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(Bill bill ) {
		return billService.delete(bill, getLoginUser());
	}


	/**
	 * 导入订单
	 */
	@Transactional(rollbackFor = Exception.class)
	@RequiresPermissions("bill:add")
	@ResponseBody
	@RequestMapping("/import")
	public JsonResult importData(MultipartFile file) throws IOException {
		List<String[]> list = PoiUtil.importData(file.getInputStream(), 1, 0);
		for (int i = 0; i < list.size(); i++) {
			String[] strs = list.get(i);
			Bill bill = new Bill();
			bill.setPackDate(strs[0]);
			bill.setCustomer(strs[1]);
			bill.setFactoryAddress(strs[2]);
			bill.setContactsPhone(strs[3]);
			bill.setVessel(strs[4]);
			bill.setVoyage(strs[5]);
			bill.setBillOfLading(strs[6]);
			bill.setBoxType(strs[7]);
			bill.setBoxNum(strs[8]);
			bill.setSealno(strs[9]);
			bill.setSerialno(strs[10]);
			bill.setCarNo(strs[11]);
			bill.setDriverPhone(strs[12]);
			bill.setRemark(strs[13]);

			// 添加
			billService.add(bill, getLoginUser());
		}
		return JsonResult.ok("导入成功");
	}

}
